package hotel.demo2.controller;

import hotel.demo2.common.domain.Hotel;
import hotel.demo2.common.domain.User;
import hotel.demo2.common.domain.UserExample;
import hotel.demo2.mapper.HotelMapper;
import hotel.demo2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@Controller
public class userController {

        @Autowired
        UserMapper userMapper;
        @RequestMapping("/toLogin")
        public String toLogin(){
            return "login";
        }
        @RequestMapping("/register")
          public  String register(){
              return "register";
        }

        @RequestMapping(value = "/toRegister", method = RequestMethod.POST)
        public String toRegister(HttpServletResponse response,@RequestParam String email,@RequestParam String pwd,@RequestParam String confirmPwd)throws IOException{
                PrintWriter out = response.getWriter();
                if(pwd.equals(confirmPwd)){
                        User user=new User();
                        user.setUemail(email);
                        user.setUserPwd(pwd);
                        userMapper.insertSelective(user);
                        out.print("<script language=\"javascript\">alert('登录成功！');window.location.href='/你的工程名/login'</script>");
                        return "/login";
                }
//                else{
//                        out.print("<div class=\"alert alert-danger alert-dismissible\">\n" +
//                                "<button class=\"close\" aria-hidden=\"true\" type=\"button\" data-dismiss=\"alert\">×</button>\n" +
//                                "<h4><i class=\"icon fa fa-ban\"></i> Alert!</h4>\n" +
//                                "Danger alert preview. This alert is dismissable. A wonderful serenity has taken possession of my entire\n" +
//                                "soul, like these sweet mornings of spring which I enjoy with my whole heart.\n" +
//                                "</div>");
//
//                        return "/toRegister";
//                }
                return "/index";
        }

        @RequestMapping("/checkIn")
        public String checkLogin(HttpServletRequest httpServletRequest, @RequestParam String email, @RequestParam String password){
                UserExample userExample=new UserExample();
                userExample.createCriteria().andUemailEqualTo(email);
                List<User> users=userMapper.selectByExample(userExample);
                User user=users.get(0);
                if(user.getUserPwd().equals(password))
                { return "/index.html";}

                return "/login.html";

        }
}
